package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    Button write, read;
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        write = findViewById(R.id.write);
        read = findViewById(R.id.read);
        text = findViewById(R.id.tv_file);
        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileOutputStream out = openFileOutput("one", MODE_PRIVATE);
                    String str = "nihaowoshixiaokeli";
                    out.write(str.getBytes());
                    out.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream in = openFileInput("one");
                    byte[] bytes = new byte[1024];
                    int len = in.read(bytes);
                    String str = new String(bytes, 0, len);
                    in.close();
                    text.setText(str);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
}